package com.itheima.mall.controller;


import com.alibaba.fastjson.JSON;
import com.itheima.mall.dto.ProductDto;
import com.itheima.mall.pojo.Product;
import com.itheima.mall.service.PmsProductService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class PmsProductController {
    @Autowired
    private PmsProductService pmsProductService;

    //创建商品
    @PostMapping("/create")
    public ResponseEntity createProduct(@RequestBody ProductDto productParam){
        pmsProductService.createProduct(productParam);
        return ResponseEntity.ok("");

    }

    //查询商品
    @GetMapping("/list")
    public ResponseEntity selectList(Integer brandId , String keyword , @RequestParam(defaultValue = "1") Integer pageNum ,
                                       @RequestParam(defaultValue = "10") Integer pageSize ,Integer productCategoryId,
                                       String productSn, Integer publishStatus ,Integer verifyStatus){
       PageResult pageResult = pmsProductService.selectList(brandId , keyword , pageNum , pageSize ,productCategoryId,
                productSn , publishStatus , verifyStatus);


        return ResponseEntity.ok(pageResult);
    }
    //根据商品名称或货号模糊查询
    @GetMapping("/simpleList")
    public ResponseEntity simpleListByKeyword(String keyword){
        List<Product> list = pmsProductService.simpleListByKeyword(keyword);
        return ResponseEntity.ok(list);
    }
    //批量修改删除状态
    @PostMapping("/update/deleteStatus")
    public ResponseEntity deleteStatusByIds( Integer deleteStatus , Integer[] ids){
        pmsProductService.deleteStatusByIds(deleteStatus , ids);
        return ResponseEntity.ok("");
    }
    //批量设置为新品
    @PostMapping("/update/newStatus")
    public ResponseEntity updateNewStatusByIds(Integer newStatus , Integer [] ids){
        pmsProductService.updateNewStatusByIds(newStatus , ids);
        return ResponseEntity.ok("");
    }
    //批量上下架商品
    @PostMapping("/update/publishStatus")
    public ResponseEntity updatePublishStatusByIds(Integer publishStatus , Integer [] ids){
        pmsProductService.updatePublishStatusByIds(publishStatus , ids);
        return ResponseEntity.ok("");
    }
    //批量商品推荐
    @PostMapping("/update/recommendStatus")
    public ResponseEntity updateRecommendStatusByIds(Integer recommendStatus , Integer [] ids){
        pmsProductService.updateRecommendStatusByIds(recommendStatus , ids);
        return ResponseEntity.ok("");
    }
    //批量修改审核状态
    @PostMapping("/update/verifyStatus")
    public ResponseEntity updateVerifyStatusByIds(Integer verifyStatus , Integer [] ids ,String  detail){
        pmsProductService.updateVerifyStatusByIds(verifyStatus , ids , detail);
        return ResponseEntity.ok("");
    }
    //更新商品
    @PostMapping("/update/{id}")
    public ResponseEntity updateById(@PathVariable("id") Integer id , @RequestBody Product productParam){
        pmsProductService.updateById(id , productParam);
        return ResponseEntity.ok("");
    }
    //根据id 获得商品信息
    @GetMapping("/updateInfo/{id}")
    public ResponseEntity updateInfoById(@PathVariable("id") Integer id){
       Product product = pmsProductService.updateInfoById(id);
        return ResponseEntity.ok(product);
    }

    //根据ids 获得商品信息map集合
    @PostMapping("/getProductByIds")
    public String getProductByIds(@RequestParam List<Long> pIds){
        List<Product> list = pmsProductService.getProductByIds(pIds);
        return JSON.toJSONString(list.toArray());
    }

}
